package com.stonedt.spider.controller.spider;

import com.stonedt.spider.aop.AuthorizationInterceptor;
import com.stonedt.spider.aop.Token;
import com.stonedt.spider.controller.BaseController;
import com.stonedt.spider.entity.SpiderFlowTask;
import com.stonedt.spider.service.SpiderService;
import com.stonedt.spider.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/task")
public class TaskController extends BaseController {

    @Autowired
    private TaskService taskService;

    @Autowired
    private SpiderService spiderService;

    @Token
    @GetMapping("/page")
    public Object task_page(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                @RequestParam(value = "pageSize",required = false,defaultValue = "10")int pageSize,
                                @RequestParam(value = "status",required = false)Integer status,
                                @RequestParam(value = "level",required = false)Integer level,
                                @RequestParam(value = "keyword",required = false)String keyword){
        try {
            if(pageNum>0 && pageSize>0){
                int beginIndex = (pageNum-1)*pageSize;
                String limit = "limit "+beginIndex+","+pageSize;
                return success_int(200,"查询成功!",taskService.taskPage(limit,status,level,keyword));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return error_int(500,"查询失败, 请检查参数是否有误！","");
    }

    @Token
    @GetMapping("/info")
    public Object task_info(@RequestParam(value = "id",required = false,defaultValue = "0")int id){
        try {
            if(id>0 ){
                return success_int(200,"查询成功!",taskService.taskInfo(id));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return error_int(500,"查询失败, 请检查参数是否有误！","");
    }

    @Token
    @GetMapping("/open")
    public Object task_open(@RequestParam(value = "id",required = false,defaultValue = "0")int id,
                                @RequestParam(value = "status",required = false)Integer status,
                                HttpServletRequest request){
        try {
            String userInfo = request.getAttribute(AuthorizationInterceptor.USER_INFO).toString();
            int userId = getUserId(userInfo);
            String userName = getUserName(userInfo);
            if(id>0 && status!=null && userId>0 && userName!=null){
                SpiderFlowTask task = new SpiderFlowTask();
                task.setId(id);
                task.setSeed_status(status);
                task.setUpdate_user(userName);
                task.setUpdate_user_id(userId);
                int type = taskService.updateTask(task);
                if(type>0){
                    SpiderFlowTask spiderFlowTask = taskService.taskInfo(id);
                    if( spiderFlowTask.getSeed_status()!=null && spiderFlowTask.getSeed_status()==1 && spiderFlowTask.getCron()!=null){
                        spiderService.checkAddTaskSpiderByCron(spiderFlowTask.getCron());
                    }
                    return success_int(200,"更新成功!",type);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return error_int(500,"更新失败, 请检查参数是否有误！","");
    }


    @Token
    @PostMapping("/save")
    public Object task_save(@RequestBody SpiderFlowTask task,
                                HttpServletRequest request){
        try {
            String userInfo = request.getAttribute(AuthorizationInterceptor.USER_INFO).toString();
            int userId = getUserId(userInfo);
            String userName = getUserName(userInfo);
            if(task!=null  && userId>0 && userName!=null ){
                task.setUpdate_user_id(userId);
                task.setUpdate_user(userName);
                int type = 0;
                if(task.getId()==null){
                    if( task.getSeed_name()!=null){
                        task.setCreate_user_id(userId);
                        task.setCreate_user(userName);
                        type = taskService.saveTask(task);
                    }
                }else {
                    if(task.getId()>0){
                        type = taskService.updateTask(task);
                    }
                }
                if(type>0 ) {
                    if( task.getSeed_status()!=null && task.getSeed_status()==1 && task.getCron()!=null){
                        spiderService.checkAddTaskSpiderByCron(task.getCron());
                    }
                    return success_int(200,"保存成功!",type);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return error_int(500,"保存失败, 请检查参数是否有误！","");
    }

    @Token
    @GetMapping("/remove")
    public Object task_open(@RequestParam(value = "id",required = false,defaultValue = "0")int id,
                            HttpServletRequest request){
        try {
            String userInfo = request.getAttribute(AuthorizationInterceptor.USER_INFO).toString();
            int userId = getUserId(userInfo);
            String userName = getUserName(userInfo);
            if(id>0  && userId>0 && userName!=null){
                SpiderFlowTask task = new SpiderFlowTask();
                task.setId(id);
                task.setIs_del(1);
                task.setUpdate_user(userName);
                task.setUpdate_user_id(userId);
                int type = taskService.updateTask(task);
                if(type>0){
                    return success_int(200,"删除成功!",type);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return error_int(500,"删除失败, 请检查参数是否有误！","");
    }

}
